package com.captain.leetcode.动态规划;

/**
 * @author captain
 * @Des
 * @Date 2021/8/1 11:22
 */
public class 最小路径和64 {
    public static void main(String[] args) {
        int[][] arr = {{1,2,3}, {4,5,6}};

        System.out.println(new 最小路径和64().minPathSum(arr));


    }

    public int minPathSum(int[][] grid) {
        //dp[i][j] dp[i][j] 时的路径和

//        [1][2][3]
//        [4][5][6]
//        [7][8][9]
        int m = grid.length;
        int n = grid[0].length;
        int[][] dp = new int[m + 1][n + 1];

        //初始化
        int initNum = 0;
        for (int i = 0; i < m; i++) {
            int num = grid[i][0];
            initNum += num;
            dp[i][0] = initNum;
        }
        initNum = 0;

        for (int i = 0; i < n; i++) {
            int num = grid[0][i];
            initNum += num;
            dp[0][i] = initNum;
        }
        int minValue = 0;
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                dp[i][j] = Math.min(dp[i - 1][j] + grid[i][j], dp[i][j - 1] + grid[i][j]);
            }
        }
        return dp[m - 1][n - 1];
    }
}
